//
//  JFTCHECKViewController.m
//  JftMergeSDK
//
//  Created by mac on 2017/9/19.
//  Copyright © 2017年 HLZ. All rights reserved.
//

#import "JFTCheckViewController.h"
#import "JFTConfigureHeader.h"
#import "NSData+JFTBase64.h"
#import "JFTDES.h"
#import "NSData+AES.h"
@interface JFTCheckViewController ()
@property (nonatomic,strong) UIImageView  *head_portrait_img;
@property (nonatomic,strong) UILabel      *lable_tip1;
@property (nonatomic,strong) UILabel      *lable_tip2;
@property (nonatomic,strong) UIButton     *p_button;
@property (nonatomic,strong) UIButton     *un_button;

@end

@implementation JFTCheckViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
//    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.head_portrait_img =[[UIImageView alloc]init];
    self.head_portrait_img.contentMode =  UIViewContentModeScaleAspectFill;
    self.head_portrait_img.image =  [UIImage imageNamed:@"tp"];
    [self.view addSubview:self.head_portrait_img];
    
    self.lable_tip1 =[[UILabel alloc]init];
    
    self.lable_tip1.text =@"1.如果你已完成，请点击“已完成付款”";
    self.lable_tip1.textAlignment =NSTextAlignmentCenter;
    [self.lable_tip1 setFont:[UIFont systemFontOfSize:15]];
    self.lable_tip1.textColor =[UIColor lightGrayColor];
    [self.view addSubview:self.lable_tip1];
    
   self.lable_tip2 =[[UILabel alloc]init];
    self.lable_tip2.text =@"2.如果你未完成，请点击“未完成付款”";
    self.lable_tip2.textColor =[UIColor lightGrayColor];
    self.lable_tip2.textAlignment =NSTextAlignmentCenter;
    [self.lable_tip2 setFont:[UIFont systemFontOfSize:15]];
    [self.view addSubview:self.lable_tip2];
    
    self.p_button =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.p_button setBackgroundColor:HexRGB(0x4894e8)];
    [self.p_button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.p_button setTitle:@"已完成付款" forState:UIControlStateNormal];
    
    [self.p_button addTarget:self action:@selector(checkAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.p_button];
    
    
    
    self.un_button =[UIButton buttonWithType:UIButtonTypeCustom];
    [self.un_button setBackgroundColor:[UIColor whiteColor]];
    self.un_button.layer.borderWidth =1;
    self.un_button.layer.borderColor =HexRGB(0x4894e8).CGColor;
    [self.un_button setTitleColor:HexRGB(0x4894e8) forState:UIControlStateNormal];
    [self.un_button setTitle:@"未完成付款" forState:UIControlStateNormal];
    
    [self.un_button addTarget:self action:@selector(checkAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.un_button];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
    if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight || [UIApplication sharedApplication].statusBarOrientation ==UIInterfaceOrientationLandscapeLeft) {
        [self viewLeft];

    }
        if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown) {
            [self viewPortrait];
        }
    // Do any additional setup after loading the view.
}
- (void)statusBarOrientationChange:(NSNotification *)notification {
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationLandscapeRight || orientation ==UIInterfaceOrientationLandscapeLeft) {
        [self viewLeft];
    }
    if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) {
        [self viewPortrait];
    }
}
- (void)viewPortrait{
    self.head_portrait_img.frame = CGRectMake((JFTSCREEN_WIDTH-106)/2, 87.5, 106, 106);
    self.lable_tip1.frame =  CGRectMake(30, 106+41+87.5, JFTSCREEN_WIDTH-60, 30);
    self.lable_tip2.frame = CGRectMake(30, 106+41+32+87.5, JFTSCREEN_WIDTH-60, 30);
    self.p_button.frame = CGRectMake(50, 106+41+32+87.5+72,JFTSCREEN_WIDTH-100, 40);
    self.un_button.frame = CGRectMake(50, 106+41+32+87.5+72+40+18,JFTSCREEN_WIDTH-100, 40);
    
}
- (void)viewLeft{
    
    self.head_portrait_img.frame = CGRectMake((JFTSCREEN_WIDTH-106)/2, 45, 106,106);
    self.lable_tip1.frame =  CGRectMake(30,106+45+15, JFTSCREEN_WIDTH-60, 30);
    self.lable_tip2.frame = CGRectMake(30,106+45+15+40, JFTSCREEN_WIDTH-60, 30);
    self.p_button.frame = CGRectMake(50, 106+40+15+40+60,(JFTSCREEN_WIDTH-120)/2, 40);
    
    self.p_button.frame = JFTISPad?CGRectMake(50, 106+40+30+40+60*2,(JFTSCREEN_WIDTH-120)/2, 40):CGRectMake(50, 106+40+15+40+60,(JFTSCREEN_WIDTH-120)/2, 40);
    self.un_button.frame = JFTISPad?CGRectMake(50+(JFTSCREEN_WIDTH-120)/2+20, 106+40+30+40+60*2,(JFTSCREEN_WIDTH-120)/2, 40):CGRectMake(50+(JFTSCREEN_WIDTH-120)/2+20, 106+40+15+40+60,(JFTSCREEN_WIDTH-120)/2, 40);
}
- (void)checkAction{
    __weak typeof(self)weakSelf =self;
    if (self.cheack_OrderId) {
        NSData* data = [[self.cheack_OrderId dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:AESKEY iv:AESIV];
        NSString * jsonBase64=[data base64EncodedString];
        jsonBase64= [JFTDES replaceNormalCharacter:jsonBase64];
        NSString*encodeUrlString= [JFTDES encodeToPercentEscapeString:jsonBase64];
        NSString*baseUrl =[[NSUserDefaults standardUserDefaults] objectForKey:BASEJFTURLKEY];
        NSString *urlString = [NSString stringWithFormat:@"%@/services/getOrderBase/getSdkOrderZFState/%@",baseUrl,encodeUrlString];
        NSURL *url = [NSURL URLWithString:urlString];
        NSURLSession *requestSesson = [NSURLSession sharedSession];
        NSURLSessionDataTask *task = [requestSesson dataTaskWithRequest:[NSURLRequest requestWithURL:url] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                NSHTTPURLResponse *ponse =(NSHTTPURLResponse*)response;
            if (error||ponse.statusCode!=200) {
                weakSelf.openComplete(@"2");
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf dismissViewControllerAnimated:NO completion:nil];
                });
                return ;
            }
            if (data.length) {
                NSString *ciphertextString =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
                NSString *ciphertext =[JFTDES replaceSpecialcharacter:ciphertextString];
                NSData *decode_data = [[NSData dataFromBase64String:ciphertext] AES256DecryptWithKey:AESKEY iv:AESIV];
                if (!decode_data) {
            weakSelf.openComplete(@"2");
            dispatch_async(dispatch_get_main_queue(), ^{
                        [weakSelf dismissViewControllerAnimated:NO completion:nil];
                    });
                    return;
                }
                NSError      *jsonError=nil;
                NSDictionary *json_dic =nil;
                if ([[NSJSONSerialization
                      JSONObjectWithData:decode_data
                      options:NSJSONReadingMutableContainers
                      error:&jsonError] isKindOfClass:[NSDictionary class]]) {
                    json_dic = [NSJSONSerialization
                            JSONObjectWithData:decode_data
                            options:NSJSONReadingMutableContainers
                            error:&jsonError];
                    NSString *flag = [json_dic objectForKey:@"flag"];
                    //NSString *msg  =[ddic objectForKey:@"msg"];
                    weakSelf.openComplete(flag);
                     dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf dismissViewControllerAnimated:NO completion:nil];
                     });
                }
                
            }
            
        }];
        [task resume];
    }
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
